
package pe.com.yugi.DAO.Impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pe.com.yugi.DAO.TipoCartaDAO;
import pe.com.yugi.Dominio.TipoCarta;
import pe.com.yugi.Util.HibernateUtil;


public class TipoCartaDAOImpl implements TipoCartaDAO, Serializable{

    @Override
    public boolean agregar(TipoCarta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public boolean modificar(TipoCarta c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public List<TipoCarta> listarTipoCartas() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from TipoCarta order by nombre asc");
        List<TipoCarta> list = query.list();
        return list;
    }

    @Override
    public TipoCarta obtenerTipoCarta(int idTipoCarta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from TipoCarta where idTipo="+idTipoCarta+"  order by nombre asc");
        TipoCarta list = (TipoCarta) query.uniqueResult();
        return list;
    }
    
}
